package com.dcbeida.activity.login;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.ResultAllModel;
import com.dcbeida.bean.User;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.LoginModel;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class LoginPresenter implements LoginContract.Present {


    private LoginContract.View view;

    private LoginModel model;

    private Activity act;

    public LoginPresenter(LoginContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = LoginModel.newInstance();
        this.act = act;

    }

    @Override
    public void sendSmsCode() {
        if (checkVaildTel()) {
            model.sendMsmCode(view.getAccount(), new LgoinCallBack(act, Object.class, lis));
        }
    }

    @Override
    public void login(int type) {
        if (validator()) {
            if(type ==0) {
                model.loginbysmscode(view.getAccount(), view.getPassword(), new LgoinCallBack(act, User.class, lis));
            }else if(type ==1){
                model.loginbyPassword(view.getAccount(), view.getPassword(), new LgoinCallBack(act, User.class, lis));
            }
        }
    }

    @Override
    public void start() {

    }

    /**
     * 登录参数校验
     *
     * @return
     */


    private boolean checkVaildTel() {
        if (TextUtils.isEmpty(view.getAccount()) && view.getAccount().length() < 11) {
            view.loginError("账号不对", ACCOUNTNOERROR);
            return false;
        }
        return true;
    }

    private boolean validator() {
        if (TextUtils.isEmpty(view.getAccount())
        ) {
            view.loginError("账号不对", ACCOUNTNOERROR);
            return false;
        }

        if (TextUtils.isEmpty(view.getPassword())) {
            view.loginError("密码不能为空", ACCOUNTPSERROR);
            return false;
        }

        return true;
    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    final class LgoinCallBack<T> extends DialogCallback<T> {

        public LgoinCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            view.dissP();
             if (data != null) {
                if (data instanceof User) {
                    MyApplication.user = (User) data;
                    view.loginSuccess();
                }else{
                    view.sendCodeStatus(1);
                }
            }

        }

        @Override
        public void showError(String message) {
            view.showError(message);
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.dissP();
            view.showTip("网络异常，请重试");

        }
    }


}
